﻿@using System.Globalization;
@{
    var _itemsCreation = new Action<NavigableViewFactory>(items =>
    {
        items.AddValue(CultureInfo.GetCultureInfo("ar").DisplayName, "ar");
        items.AddValue(CultureInfo.GetCultureInfo("bg").DisplayName, "bg");
        items.AddValue(CultureInfo.GetCultureInfo("zh-CHS").DisplayName, "zh-CHS");
        items.AddValue(CultureInfo.GetCultureInfo("zh-CHT").DisplayName, "zh-CHT");
        items.AddValue(CultureInfo.GetCultureInfo("cs").DisplayName, "cs");
        items.AddValue(CultureInfo.GetCultureInfo("da").DisplayName, "da");
        items.AddValue(CultureInfo.GetCultureInfo("nl").DisplayName, "nl");
        items.AddValue(CultureInfo.GetCultureInfo("en").DisplayName, "en");
        items.AddValue(CultureInfo.GetCultureInfo("et").DisplayName, "et");
        items.AddValue(CultureInfo.GetCultureInfo("fi").DisplayName, "fi");
        items.AddValue(CultureInfo.GetCultureInfo("fr").DisplayName, "fr");
        items.AddValue(CultureInfo.GetCultureInfo("de").DisplayName, "de");
        items.AddValue(CultureInfo.GetCultureInfo("el").DisplayName, "el");
        items.AddValue(CultureInfo.GetCultureInfo("he").DisplayName, "he");
        items.AddValue(CultureInfo.GetCultureInfo("hu").DisplayName, "hu");
        items.AddValue(CultureInfo.GetCultureInfo("id").DisplayName, "id");
        items.AddValue(CultureInfo.GetCultureInfo("it").DisplayName, "it");
        items.AddValue(CultureInfo.GetCultureInfo("ja").DisplayName, "ja");
        items.AddValue(CultureInfo.GetCultureInfo("ko").DisplayName, "ko");
        items.AddValue(CultureInfo.GetCultureInfo("lv").DisplayName, "lv");
        items.AddValue(CultureInfo.GetCultureInfo("lt").DisplayName, "lt");
        items.AddValue(CultureInfo.GetCultureInfo("no").DisplayName, "no");
        items.AddValue(CultureInfo.GetCultureInfo("pl").DisplayName, "pl");
        items.AddValue(CultureInfo.GetCultureInfo("pt").DisplayName, "pt");
        items.AddValue(CultureInfo.GetCultureInfo("ro").DisplayName, "ro");
        items.AddValue(CultureInfo.GetCultureInfo("ru").DisplayName, "ru");
        items.AddValue(CultureInfo.GetCultureInfo("sk").DisplayName, "sk");
        items.AddValue(CultureInfo.GetCultureInfo("sl").DisplayName, "sl");
        items.AddValue(CultureInfo.GetCultureInfo("es").DisplayName, "es");
        items.AddValue(CultureInfo.GetCultureInfo("sv").DisplayName, "sv");
        items.AddValue(CultureInfo.GetCultureInfo("th").DisplayName, "th");
        items.AddValue(CultureInfo.GetCultureInfo("tr").DisplayName, "tr");
        items.AddValue(CultureInfo.GetCultureInfo("uk").DisplayName, "uk");
        items.AddValue(CultureInfo.GetCultureInfo("vi").DisplayName, "vi");
    });
    string appID = System.Web.Configuration.WebConfigurationManager.AppSettings["BingAppID"];
}
@{
    Ajax.Dna().Widget()
        .Content(@<text>
    @{
        if (!string.IsNullOrEmpty(item.GetString("AppIdToken")))
        {
            appID = item.GetString("AppIdToken");
        }
    }
    @if (!string.IsNullOrEmpty(appID))
    {
        <table>
            <tr>
                <td>
                    Source:
                </td>
                <td>
                    @Ajax.Dna().ComboBox().Width(150).Name(item.GenerateFieldID("languageFrom")).Select("en").Items(_itemsCreation).GetHtml()
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    @Ajax.Dna().TextArea().Name(item.GenerateFieldID("source")).GetHtml()
                </td>
            </tr>
            <tr>
                <td>
                    Target:
                </td>
                <td>
                    @Ajax.Dna().ComboBox().Width(150).Name(item.GenerateFieldID("languageTo")).Select("zh-CHS").Items(_itemsCreation).GetHtml()
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    @Ajax.Dna().TextArea().Name(item.GenerateFieldID("result")).GetHtml()
                </td>
            </tr>
        </table>
        if (!string.IsNullOrEmpty(appID))
        {
        <p>
            @Ajax.Dna().Button(item.GenerateFieldID("translate")).Text("Translate").Click("translate();").GetHtml()
        </p>
        }
        else
        {
        <p>
            Please get the Bing application ID and set to AppTokenID.</p>
        }
        <script type="text/javascript">
        function translate() {
            window.mycallback = function (response) { 
              $("#@item.GenerateFieldID("result")").val(response);
            }

            var text = encodeURIComponent($("#@item.GenerateFieldID("source")").val());
            var languageFrom =$("#@item.GenerateFieldID("languageFrom")").val();
            var languageTo =$("#@item.GenerateFieldID("languageTo")").val();
  
            if ($("#callScript").length) $("#callScript").remove();
              var s = document.createElement("script");
              s.setAttribute("id","callScript");
            s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=@(appID)&from="+ languageFrom + "&to=" + languageTo + "&text=" + text;
            $("head").append(s);
        }
        </script>
    }
    </text>)
        .Render();
}
